package com.itheima.servlet;

import com.itheima.bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
    学生登录
 */
@WebServlet("/loginStudentServlet")
public class LoginStudentServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        //判断是否正确
        if ("zhangsan".equals(username) && "123".equals(password)){
            //登录成功
            User user = new User(username,password);
            req.getSession().setAttribute("user",user);
            //直接跳转到首页
            resp.sendRedirect(req.getContextPath() + "/index.jsp");
        }else{
            //登录失败

            req.setAttribute("msg","用户名或密码错误");
            //转发回去
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
